<?php
require_once 'class/CompanyServiceClient.php';
require_once 'class/WebServices.php';

function GetValue($fields, $name) {
	if (array_key_exists($name, $fields))
		return $fields[$name];
	return '';
}

function GetSelect($fields, $name, $val) {
	if (array_key_exists($name, $fields) && $fields[$name] == $val)
		return ' selected="selected"';
	return '';
}

$fields = array('HoTen', 'DiaChi', 'NgaySinh', 'Phai', 'Luong', 'CongTy');

// prepare filter
$filters = array();
foreach ($_POST as $key => $value) {
	if (in_array($key, $fields) && $value != '' && $value != -1) {
		$filters[$key] = $value;
	};
}
$emps = CompanyServiceClient::Select($filters);
$providers = WebServices::Select();

$no = 1;

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="vi" xml:lang="vi" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link type="text/css" href="script/datepicker/css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />	
	<link type="text/css" href="style/index.css" rel="stylesheet" />
	
	<script type="text/javascript" src="script/jquery-1.7.1.min.js"></script>
	<script type="text/javascript" src="script/datepicker/js/jquery-ui-1.8.16.custom.min.js"></script>
	<script type="text/javascript" src="script/index.js"></script>
	<script type="text/javascript">
		$(function () {
			$('#NgaySinh').datepicker({ inline: true, dateFormat: "dd/mm/yy"});
		});
	</script>
	
	<title>SearchPortal</title>
</head>
<body>
	<h1>SearchPortal</h1>
	<div class="box">
		<div class="heading">
			<h1><img alt="" src="image/user.png" />Nhân viên</h1>
			<div class="buttons">
				<a class="button" href="javascript: document.nhanvien.submit()" >Tìm kiếm</a>
				<a class="button" href="javascript: reset()" >Làm lại</a>
			</div>
		</div>
		<div class="content">
			<!-- search area -->
			<form name="nhanvien" method="post">
			<fieldset>
			<ul class="cols">
				<li>
				<div class="field">
					<div class="label"><label for="HoTen">Họ tên</label></div>
					<div class="input"><input type="text" id="HoTen" name="HoTen" value="<?php echo GetValue($filters, 'HoTen')?>" />
					</div>
				</div>
				<div class="field">
					<div class="label"><label for="NgaySinh">Ngày sinh</label></div>
					<div class="input"><input type="text" id="NgaySinh" name="NgaySinh" value="<?php echo GetValue($filters, 'NgaySinh')?>" />
					</div>
				</div>
				<div class="field">
					<div class="label"><label for="DiaChi">Địa chỉ</label></div>
					<div class="input"><input type="text" id="DiaChi" name="DiaChi" value="<?php echo GetValue($filters, 'DiaChi')?>" />
					</div>
				</div>
				</li>
				<li>
				<div class="field">
					<div class="label">Phái</div>
					<div class="input">
						<select name="Phai" id="Phai">
							<option value="-1">Tất cả</option>
							<option <?php echo GetSelect($filters, 'Phai', 0)?> value="0">Nam</option>
							<option <?php echo GetSelect($filters, 'Phai', 1)?> value="1">Nữ</option>
						</select>
					</div>
				</div>
				<div class="field">
					<div class="label"><label for="Luong">Lương</label></div>
					<div class="input"><input type="text" id="Luong" name="Luong" value="<?php echo GetValue($filters, 'Luong')?>" />
					</div>
				</div>
				<div class="field">
					<div class="label">Công ty</div>
					<div class="input">
						<ul class="CongTy">
						<?php foreach ($providers as $provider): ?>
							<li>
								<input type="checkbox" name="CongTy[]" 
									value="<?php echo $provider['ID']?>" 
									id="CongTy<?php echo $provider['Company']?>"
									<?php if(array_key_exists('CongTy', $filters) && in_array($provider['ID'], $filters['CongTy'])) echo " checked=checked"?> />
								<label for="CongTy<?php echo $provider['Company']?>"><?php echo $provider['Company']?></label>
							</li>
						<?php endforeach; ?>
						</ul>
					</div>
				</div>
				</li>
			</ul>
			</fieldset>
			</form>

			<table class="list">
				<tr>
					<th>STT</th>
					<th>Họ tên</th>
					<th>Ngày sinh</th>
					<th>Địa chỉ</th>
					<th>Phái</th>
					<th>Lương</th>
					<th>Công ty</th>
				</tr>
				
				<?php if(count($emps)>0): ?>
				<?php foreach ($emps as $emp): ?>
				<tr>
					<td><?php echo $no; $no++; ?></td>
					<td><?php echo $emp['HoTen']?></td>
					<td><?php echo $emp['NgaySinh']?></td>
					<td><?php echo $emp['DiaChi']?></td>
					<?php $gender = ($emp['Phai']==0)? 'Nam': 'Nữ'; ?>		
					<td><?php echo $gender?></td>
					<td><?php echo $emp['Luong']?></td>
					<td><?php echo $emp['Company']?></td>
				</tr>
				<?php endforeach; ?>
				<?php else: ?>
				<tr>
					<td class="empty" colspan="7">Không tìm thấy kết quả nào khớp</td>
				</tr>
				<?php endif; ?>
			</table>
		</div>
	</div>
	
</body>
</html>
